package com.subway.babynote.syncdata;

import java.sql.Date;

import java.text.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;




public class StringUtil {
	public static String floats2str(List<Float> l)
	{
		
		StringBuffer sb = new StringBuffer(); 
		Iterator<Float> it = l.iterator();
		int i = 0;
		while(it.hasNext())
		{
			if(i>0)
				sb.append(",");
			i++;
			sb.append(it.next().floatValue());
		}
		return sb.toString();
	}
	
	public static List<Float> str2floats(String s)
	{
		String[] ss = s.split(",");
		List<Float> l = new ArrayList<Float>();
		for(int i=0;i<ss.length;i++)
		{
			l.add(str2float(ss[i]));
		}
		return l;
	}
	
	public static Float str2float(String str)
	{
		Float f = new Float(0);
		try
		{
			if(str!=null)
			{
				f = Float.parseFloat(str);
			}
			return f;
		}
		catch(Exception e)
		{
			 System.out.println("Int ParseException: "+e.getMessage());
			 return f;
		}
	}
	public static int str2int(String str)
	{
		int i = -1;
		try
		{
			if(str!=null)
			{
				i = Integer.parseInt(str);
			}
			return i;
		}
		catch(Exception e)
		{
			 System.out.println("Int ParseException: "+e.getMessage());
			 return -1;
		}
	}
	public static String int2str(int i)
	{
		return Integer.toString(i);		
	}
	public static Date sql_str2date(String str)
	{
		return str2date(str,PostParam.DateFormatString.sqlDateFormat);
	}
	public static Date str2date(String str) 
	{
		return str2date(str,PostParam.DateFormatString.simpleDateFormat);
	}
	public static java.sql.Date onlyday(java.sql.Date dt)
	{
		java.util.Date dt1 = new java.util.Date(dt.getTime());
		Calendar c = Calendar.getInstance();
		c.setTime(dt1);
		Calendar e = Calendar.getInstance();		
		e.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),0,0,0);		
		dt1 = e.getTime();
		return new java.sql.Date(dt1.getTime());		
	}
	private static Calendar get_calendar()
	{
		return Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"), Locale.CHINA);
	}
	public static int now()
	{
		Calendar c = get_calendar();
		long t =  c.getTime().getTime() / 1000;
		return (int)t;
	}
	
	public static int getmonth(int dt)
	{
		java.sql.Date d = int2date(dt);
		return getmonth(d);
	}
	
	public static int getmonth(java.sql.Date dt)
	{
		java.util.Date dt1 = new java.util.Date(dt.getTime());
		Calendar c = Calendar.getInstance();
		c.setTime(dt1);
		Calendar e = Calendar.getInstance();
		int y1 = c.get(Calendar.YEAR);
		int m1 = c.get(Calendar.MONTH);
		int d1 = c.get(Calendar.DAY_OF_MONTH);
		
		int y2 = e.get(Calendar.YEAR);
		int m2 = e.get(Calendar.MONTH);
		int d2 = e.get(Calendar.DAY_OF_MONTH);
		
		int y3 = y2 - y1;
		int m3 = m2 - m1;
		int d3 = d2 - d1;
		
		int m = y3 * 12 + m3 + (d3<0?-1:0);
		return m;
		
	}
	
	private static Date str2date(String str, String format) 
	{
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			if(str!=null)
			{
				java.util.Date dt = sdf.parse(str);
				return new java.sql.Date(dt.getTime());
			}
			else
				return null;
		}
		catch(ParseException pe)
		{
			 System.out.println("ParseException: "+pe.getMessage());
			 return null;
		}
	}
	public static int date2int(java.sql.Date dt)
	{
		if(dt == null)
			return 0;
		else
			return (int) (dt.getTime()/1000);
	}
	public static java.sql.Date int2date(int t)
	{
		long tl = t;
		if(t>0)
			return new java.sql.Date((long)tl*1000);
		else
			return null;
	}
	public static String date2str(java.sql.Date dt) 
	{
		return date2str(dt,PostParam.DateFormatString.simpleDateFormat);
	}
	public static String sql_date2str(java.sql.Date dt) 
	{
		return date2str(dt,PostParam.DateFormatString.sqlDateFormat);
	}
	private static String date2str(java.sql.Date dt, String format)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			java.util.Date dt1= new java.util.Date(dt.getTime());
			String str= sdf.format(dt1);
			return str;
		}
		catch(Exception pe)
		{
			 System.out.println("date2str ex: "+pe.getMessage());
			 return null;
		}
	}
	/*
	public static Time str2time(String str)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(PostParam.DateFormatString.simpleDateFormat);
		try
		{
			if(!str.isEmpty())
			{
				java.util.Date dt = sdf.parse(str);
				return new java.sql.Time(dt.getTime());
			}
			else
				return null;
		}
		catch(ParseException pe)
		{
			 System.out.println("ParseException: "+pe.getMessage());
			 return null;
		}
		
	}
	*/
	public static boolean str2bool(String str)
	{
		boolean b = true;
		try
		{
			if(str==null)
				b = false;
			else
			{
				int i = Integer.parseInt(str);
				if(i==0)
					b = false;
				else if(i==1)
					b = true;
				else
					b=false;
			}		
		}
		catch(Exception e)
		{
			 System.out.println("Int ParseException: "+e.getMessage());
			 b=false;
		}
		return b;
		
	}
	public static String bool2str(boolean b)
	{
		int i = b? 1: 0;
		return int2str(i);		
	}
	
	public static String float2str(float f)
	{
		return Float.toString(f);		
	}
	
	public static String bytes2Str(byte[] b, int offset, int len)
	{
		try
		{
			String str = null;
			str = new String(b,offset, len,"UTF-8");
			return str;
		}
		catch(Exception e)
		{
			 System.out.println("bytes2Str: "+e.getMessage());
			 return "";
		}
	}
	
	public static byte[] str2bytes(String str)
	{
		try
		{
		return str.getBytes("UTF-8");
		}
		catch(Exception e)
		{
			 System.out.println("bytes2Str: "+e.getMessage());
			 return null;
		}
	}
	
	/*
	public static String getParam(HttpServletRequest req, String param)
	{
		try
		{
			return req.getParameter(param);
		}
		catch(Exception e)
		{
			 System.out.println("Int ParseException: "+e.getMessage());
			 return "";
		}		
	}
	
	
*/
}
